package com.im.service.message.controller;

import com.im.common.model.RestResponse;
import com.im.common.model.SyncReq;
import com.im.common.model.message.CheckSendMessageReq;
import com.im.service.message.model.req.SendMessageReq;
import com.im.service.message.service.MessageSyncService;
import com.im.service.message.service.P2PMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/message")
@Api(tags = "消息控制器")
public class MessageController {

    @Autowired
    private P2PMessageService p2PMessageService;
    @Autowired
    private MessageSyncService messageSyncService;

    @ApiOperation(value = "发送消息")
    @PostMapping("/send")
    public RestResponse send(@RequestBody @Validated SendMessageReq req, Integer appId) {
        req.setAppId(appId);
        return RestResponse.success(p2PMessageService.send(req));
    }

    @ApiOperation(value = "检查发送消息权限")
    @PostMapping("/checkSend")
    public RestResponse checkSend(@RequestBody @Validated CheckSendMessageReq req) {
        p2PMessageService.imServerPermissionCheck(req.getFromId(), req.getToId(), req.getAppId());
        return RestResponse.success();
    }

    @ApiOperation(value = "同步离线消息")
    @PostMapping("/syncOfflineMessage")
    public RestResponse syncOfflineMessage(@RequestBody @Validated SyncReq req, Integer appId) {
        req.setAppId(appId);
        return RestResponse.success(messageSyncService.syncOfflineMessage(req));
    }
}

